package br.unicamp.ic.lis.dbvapi.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "MultiversionObject")
public class MultiversionObject implements Serializable,
		Comparable<MultiversionObject> {

	private static final long serialVersionUID = 4710071355279963453L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "oid")
	private long id;

	private String title;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "MVO_PV", joinColumns = @JoinColumn(name = "oid"), inverseJoinColumns = @JoinColumn(name = "pvid"), uniqueConstraints = @UniqueConstraint(columnNames = {
			"oid", "pvid" }))
	private Set<AbstractPhysicalVersion<?>> physicalVersions = new HashSet<AbstractPhysicalVersion<?>>();

	public Set<AbstractPhysicalVersion<?>> getPhysicalVersions() {
		return physicalVersions;
	}

	public void setPhysicalVersions(
			Set<AbstractPhysicalVersion<?>> physicalVersions) {
		this.physicalVersions = physicalVersions;
	}

	public void addPhysicalVersion(AbstractPhysicalVersion<?>... pv) {
		for (AbstractPhysicalVersion<?> p : pv) {
			physicalVersions.add(p);
		}
	}

	public void removePhysicalVersion(AbstractPhysicalVersion<?>... pv) {
		for (AbstractPhysicalVersion<?> p : pv) {
			physicalVersions.remove(p);
		}
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public String toString() {
		return "MultiversionObject [id=" + id + ", title=" + title
				+ ", physicalVersions=" + physicalVersions + "]";
	}

	@Override
	public int compareTo(MultiversionObject other) {
		return this.title.compareTo(other.title);
	}
}
